package controller;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

import model.BookDetailsDTO;

import DAO.BookDetailsDAO;
import DAO.DAOFactory;
import DAO.NotFoundException;


/**
 * Servlet implementation class CSVController
 */
@WebServlet(description = "to download data", urlPatterns = { "/CSVController" })
public class CSVController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CSVController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request, response);
	}

	private void doProcess(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		ArrayList<BookDetailsDTO> table;
		BookDetailsDAO f = DAOFactory.loadInstance().getBookDetailsDAO();
		//HttpSession session= request.getSession();

		FileWriter fw = new FileWriter("C:\\Users\\Ping\\Documents\\Historyfile.csv");
		PrintWriter pw = new PrintWriter(fw);
		pw.append("BookingID");
		pw.append(',');
		pw.append("Emergency");
		pw.append(',');
		pw.append("Priority");
		pw.append(',');
		pw.append("FromDate");
		pw.append(',');
		pw.append("ToDate");
		pw.append(',');
		pw.append("Reason");
		pw.append(',');
		pw.append("FacilityName");
		pw.append(',');
		pw.append("BookedOn");
		pw.append(',');
		pw.append("BookedBy");
		pw.append(',');
		pw.append("Status");
		pw.append('\n');
		try {
			table = f.findBookingHistory();

			for(BookDetailsDTO rs :table)
			{
				pw.append(Integer.toString(rs.getBookingID()));
				pw.append(',');
				pw.append(rs.getEmergency());
				pw.append(',');
				pw.append(rs.getPriority());
				pw.append(',');
				pw.append(rs.getFromDate().toString());
				pw.append(',');
				pw.append(rs.getToDate().toString());
				pw.append(',');
				pw.append(rs.getReason());
				pw.append(',');
				pw.append(rs.getFacilityName());
				pw.append(',');
				pw.append(rs.getBookedOn().toString());
				pw.append(',');
				pw.append(Integer.toString(rs.getBookedBy()));
				pw.append(',');
				pw.append(rs.getStatus());
				pw.append('\n');
				System.out.println("CSV File is created successfully.");
			}


			fw.close();
			pw.flush();
			pw.close();
			//session.setAttribute("csv", "done");
		}		
		catch (NotFoundException e) 
		{
			e.printStackTrace();
			//session.setAttribute("csv", "not done");
		}
		catch (IOException e) 
		{
			e.printStackTrace();
			//session.setAttribute("csv", "not done");
		} finally{

			RequestDispatcher rd = request.getRequestDispatcher("/BookingHistoryController");
			rd.forward(request, response);

		}

	}
}
